package cn.dnui_wwt524.dnuifood_16110100524.fragment;


import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cn.dnui_wwt524.dnuifood_16110100524.Adapter.ShopAdapter;
import cn.dnui_wwt524.dnuifood_16110100524.R;
import cn.dnui_wwt524.dnuifood_16110100524.bean.ShopBean;
import cn.dnui_wwt524.dnuifood_16110100524.listener.OnRetrofitListener;
import cn.dnui_wwt524.dnuifood_16110100524.model.ShopModel;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends BaseFragment implements OnRetrofitListener<List<ShopBean>> {
    private RecyclerView recyclerView;
    private ShopAdapter adapter;

    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        recyclerView=view.findViewById(R.id.shop_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        initViews();
        initData();
    }
    void initViews() {
        recyclerView= (RecyclerView)recyclerView.findViewById(R.id.shop_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    }

    void initEvents() {

    }

    void initData() {
        ShopModel shopModel=new ShopModel();
        shopModel.getAllShops(this);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    @Override
    public void onSuccess(List<ShopBean> object, int flag) {
        adapter=new ShopAdapter(getActivity(),object);
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onFailure(String msg) {
        Toast.makeText(getActivity(),"网络错误",Toast.LENGTH_SHORT).show();
    }
}
